##Alison E. Post##
##"Foreign and Domestic Investment in Argentina" (2014)##
##Code for BIT and Arbitration Analysis, Tables 7.4 - 7.6, Figure 7.1###

library(foreign)
library(survival)
library(Zelig)
library(car)

##Note: all analyses for the book were originally conducted using the R package "Zelig".  After the proofs for the book were submitted, Zelig stopped supporting the Cox proportional hazard model. I have therefore added R survival package commands to obtain key table results throughout this file.## 

##load replication data set##
foreign_dom <- read.csv("arbitration.cross.section.csv", header=TRUE)

##Create subset of data with just concessions and divestitures##
div.con <- foreign_dom[foreign_dom$Type_PPI!="Greenfield Project",]
div.con <- div.con[div.con$Type_PPI!="Management, Lease Contract",]
foreign.div.con <- div.con[div.con$X25_Foreign==1,]


##Table 7.4: International Arbitration Access and Contract Cancellation Among Foreign Investors##

#Latin America#
LA.div.con <- div.con[div.con$Region=="Latin America and the Caribbean",]
nrow(LA.div.con) #120 rows#
attach(LA.div.con)
LA.pop<- data.frame(Project_Name, X25_Foreign, Type_PPI, Cancelled, BIT_Arb, BIT_Strong_Arb, Alt_Treaty_Arb, Dom_I_Law_Arb, WB_Rule_Law)
LA.pop<-na.omit(LA.pop)
nrow(LA.pop) #101 rows#
detach(LA.div.con)

LA.pop$Arbitration <- 0 #Choose subset of cases with strong arbitration provisions#
for (i in 1:nrow(LA.pop)){
	if (LA.pop$BIT_Strong_Arb[i]==1 | LA.pop$Dom_I_Law_Arb[i]=="Comprehensive" | LA.pop$Alt_Treaty_Arb[i]=="Comprehensive") 
	LA.pop$Arbitration[i] <- 1
	else
	LA.pop$Arbitration[i] <- 0
	}

for.LA.pop <- LA.pop[LA.pop$X25_Foreign==1,]
nrow(for.LA.pop) #48 contracts#
can.for.LA.pop <- for.LA.pop[for.LA.pop$Cancelled==1,]
nrow(can.for.LA.pop) #13#
Arb.for.LA.pop <- for.LA.pop[for.LA.pop$Arbitration==1,]
nrow(Arb.for.LA.pop) #31#
mean(Arb.for.LA.pop$WB_Rule_Law)
Can.Arb <- Arb.for.LA.pop[Arb.for.LA.pop$Cancelled==1,]
nrow(Can.Arb) #12#
No.Arb.for.LA.pop <- for.LA.pop[for.LA.pop$Arbitration==0,]
nrow(No.Arb.for.LA.pop) #17#
Can.noArb <- No.Arb.for.LA.pop[No.Arb.for.LA.pop$Cancelled==1,]
nrow(Can.noArb) #1#
mean(No.Arb.for.LA.pop$WB_Rule_Law)

dom.LA.pop <- LA.pop[LA.pop$X25_Foreign==0,] #For comparison rate of concellation among domestically-controlled contracts low#
nrow(dom.LA.pop) #53#
can.dom.LA.pop <- dom.LA.pop[dom.LA.pop$Cancelled==1,]
nrow(can.dom.LA.pop) #1#

#Worldwide (Low and Middle Income Countries)

attach(div.con)
chart.div.con<- data.frame(Project_Name, Region, X25_Foreign, Type_PPI, Cancelled, BIT_Arb, BIT_Strong_Arb, Alt_Treaty_Arb, Dom_I_Law_Arb, WB_Rule_Law)
chart.div.con<-na.omit(chart.div.con)
nrow(chart.div.con) #223 rows (from 263)#
detach(div.con)

	#For strong arbitration provisions#
chart.div.con$Arbitration <- 0
for (i in 1:nrow(chart.div.con)){
	if (chart.div.con$BIT_Strong_Arb[i]==1 | chart.div.con$Dom_I_Law_Arb[i]=="Comprehensive" | chart.div.con$Alt_Treaty_Arb[i]=="Comprehensive") 
	chart.div.con$Arbitration[i] <- 1
	else
	chart.div.con$Arbitration[i] <- 0
	}

for.chart.div.con <- chart.div.con[chart.div.con$X25_Foreign==1,]
nrow(for.chart.div.con) #118 contracts#
can.for.chart.div.con <- for.chart.div.con[for.chart.div.con$Cancelled==1,]
nrow(can.for.chart.div.con) #18#
Arb.for.chart.div.con <- for.chart.div.con[for.chart.div.con$Arbitration==1,]
nrow(Arb.for.chart.div.con) #43#
mean(Arb.for.chart.div.con$WB_Rule_Law) #0.04#
Can.Arb <- Arb.for.chart.div.con[Arb.for.chart.div.con$Cancelled==1,]
nrow(Can.Arb) #12#
No.Arb.for.chart.div.con <- for.chart.div.con[for.chart.div.con$Arbitration==0,]
nrow(No.Arb.for.chart.div.con) #75#
mean(No.Arb.for.chart.div.con$WB_Rule_Law) #0-.14#
Can.noArb <- No.Arb.for.chart.div.con[No.Arb.for.chart.div.con$Cancelled==1,]
nrow(Can.noArb) #1#

dom.World <- chart.div.con[chart.div.con$X25_Foreign==0,] #comparison with cancellation rates for domestic firms#
nrow(dom.World)
Arb.dom.World <-dom.World[dom.World$Arbitration==1,] 
NoArb.dom.World <-dom.World[dom.World$Arbitration==0,]


##TABLE 7.5:  International Arbitration Access and Contract Duration##
	
	#Create version of dataset without missing values for key variables#
attach(foreign.div.con)
div.con.subset <- data.frame(Project_Name, Lifespan, Cancelled, BIT_Arb, BIT_Strong_Arb, Alt_Treaty_Arb, Dom_I_Law_Arb, GDPpc1991, WB_Rule_Law, Polity, Country,Type_PPI, Region)
div.con.subset <- na.omit(div.con.subset)
detach(foreign.div.con)	
div.con.subset$Arbitration <- 0

	##Model 1:  Arbitration provision of any strength##

for (i in 1:nrow(div.con.subset)){
	if (div.con.subset$BIT_Arb[i]==1 | div.con.subset$Dom_I_Law_Arb[i]!=0 | div.con.subset$Alt_Treaty_Arb[i]!=0) 
	div.con.subset$Arbitration[i] <- 1
	else
	div.con.subset$Arbitration[i] <- 0
	}

z.out <- zelig(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + strata(Type_PPI), model="coxph", data=div.con.subset, robust=TRUE, cluster="Country")
summary(z.out)

s.out <- coxph(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + strata(Type_PPI) + cluster(Country), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)

	##Model 2: Strong version of arbitration variable##

div.con.subset$Arbitration <- 0

for (i in 1:nrow(div.con.subset)){
	if (div.con.subset$BIT_Strong_Arb[i]==1 | div.con.subset$Dom_I_Law_Arb[i]=="Comprehensive" | div.con.subset$Alt_Treaty_Arb[i]=="Comprehensive") 
	div.con.subset$Arbitration[i] <- 1
	else
	div.con.subset$Arbitration[i] <- 0
	}

z.out <- zelig(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + strata(Type_PPI), model="coxph", data=div.con.subset, robust=TRUE, cluster="Country")
summary(z.out)

s.out <- coxph(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + strata(Type_PPI) + cluster(Country), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)

	##DF betas: Observations Influential for the Arbitration Variable for Model 2##
par(mfrow=c(2,2))
rr <- resid(s.out, type='dfbeta')
plot(div.con.subset$Arbitration, rr[,1], xlab="Arbitration", ylab="Influence for Arbitration Variable") ##note that have to adjust column of residual matrix if alter regression##
identify(div.con.subset$Arbitration, rr[,1])

	##Model 2: Results do not change when exclude autocracies###
	div.con.subsetdem <- div.con.subset[div.con.subset$Polity>0,]
	s.out <- coxph(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + strata(Type_PPI) + cluster(Country), 	robust=TRUE, data=div.con.subsetdem)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)
	
	div.con.subsetdem <- div.con.subset[div.con.subset$Polity>6,]
	s.out <- coxph(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + strata(Type_PPI) + cluster(Country), 	robust=TRUE, data=div.con.subsetdem)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)

	##Model 3: Strong version of arbitration variable#

attach(foreign.div.con)
div.con.subset <- data.frame(Project_Name, Lifespan, Cancelled, BIT_Arb, BIT_Strong_Arb, Alt_Treaty_Arb, Dom_I_Law_Arb, GDPpc1991, WB_Rule_Law, Polity, Country,Type_PPI, Region, Size_Cat)
div.con.subset <- na.omit(div.con.subset)
detach(foreign.div.con)

for (i in 1:nrow(div.con.subset)){
	if (div.con.subset$BIT_Strong_Arb[i]==1 | div.con.subset$Dom_I_Law_Arb[i]=="Comprehensive" | div.con.subset$Alt_Treaty_Arb[i]=="Comprehensive") 
	div.con.subset$Arbitration[i] <- 1
	else
	div.con.subset$Arbitration[i] <- 0
	}

z.out <- zelig(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + Size_Cat + strata(Type_PPI), model="coxph", data=div.con.subset, robust=TRUE, cluster="Country")
summary(z.out)

s.out <- coxph(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + Size_Cat + strata(Type_PPI) + cluster(Country), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)

	##Model 4: Strong version of arbitration variable#  

attach(foreign.div.con)
div.con.subset <- data.frame(Project_Name, Lifespan, Cancelled, BIT_Arb, BIT_Strong_Arb, Alt_Treaty_Arb, Dom_I_Law_Arb, GDPpc1991, WB_Rule_Law, Polity, Country,Type_PPI, Region, Grantee)
div.con.subset <- na.omit(div.con.subset)
detach(foreign.div.con)

for (i in 1:nrow(div.con.subset)){
	if (div.con.subset$BIT_Strong_Arb[i]==1 | div.con.subset$Dom_I_Law_Arb[i]=="Comprehensive" | div.con.subset$Alt_Treaty_Arb[i]=="Comprehensive") 
	div.con.subset$Arbitration[i] <- 1
	else
	div.con.subset$Arbitration[i] <- 0
	}

div.con.subset$National <- div.con.subset$Grantee=="Federal"

z.out <- zelig(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + National + Arbitration*National + strata(Type_PPI), model="coxph", data=div.con.subset, robust=TRUE, cluster="Country")
summary(z.out)

s.out <- coxph(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + National + Arbitration*National + strata(Type_PPI) + cluster(Country), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)

	##Model 5: Strong version of arbitration variable# 

attach(foreign.div.con)
div.con.subset <- data.frame(Project_Name, Lifespan, Cancelled, BIT_Arb, BIT_Strong_Arb, Alt_Treaty_Arb, Dom_I_Law_Arb, GDPpc1991, WB_Rule_Law, Polity, Country,Type_PPI, Region, Joint_Venture)
div.con.subset <- na.omit(div.con.subset)
detach(foreign.div.con)

for (i in 1:nrow(div.con.subset)){
	if (div.con.subset$BIT_Strong_Arb[i]==1 | div.con.subset$Dom_I_Law_Arb[i]=="Comprehensive" | div.con.subset$Alt_Treaty_Arb[i]=="Comprehensive") 
	div.con.subset$Arbitration[i] <- 1
	else
	div.con.subset$Arbitration[i] <- 0
	}

z.out <- zelig(Surv(Lifespan, Cancelled) ~ Joint_Venture + GDPpc1991 + WB_Rule_Law + Polity + strata(Type_PPI), model="coxph", data=div.con.subset, robust=TRUE, cluster="Country")
summary(z.out)

s.out <- coxph(Surv(Lifespan, Cancelled) ~ Joint_Venture + GDPpc1991 + WB_Rule_Law + Polity + strata(Type_PPI) + cluster(Country), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)

	##Model 6: Strong version of arbitration variable#

attach(foreign.div.con)
div.con.subset <- data.frame(Project_Name, Lifespan, Cancelled, BIT_Arb, BIT_Strong_Arb, Alt_Treaty_Arb, Dom_I_Law_Arb, GDPpc1991, WB_Rule_Law, Polity, Country,Type_PPI, Region, Multi_Part)
div.con.subset <- na.omit(div.con.subset)
detach(foreign.div.con)

for (i in 1:nrow(div.con.subset)){
	if (div.con.subset$BIT_Strong_Arb[i]==1 | div.con.subset$Dom_I_Law_Arb[i]=="Comprehensive" | div.con.subset$Alt_Treaty_Arb[i]=="Comprehensive") 
	div.con.subset$Arbitration[i] <- 1
	else
	div.con.subset$Arbitration[i] <- 0
	}
	
z.out <- zelig(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + Multi_Part + strata(Type_PPI), model="coxph", data=div.con.subset, robust=TRUE, cluster="Country")
summary(z.out)

s.out <- coxph(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity + Multi_Part + strata(Type_PPI) + cluster(Country), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)
 

##First Difference Simulations for Model 2, Table 7.5 (only possible to run without stratification)##

attach(foreign.div.con)
div.con.subset <- data.frame(Project_Name, Lifespan, Cancelled, BIT_Arb, BIT_Strong_Arb, Alt_Treaty_Arb, Dom_I_Law_Arb, GDPpc1991, WB_Rule_Law, Polity, Country,Type_PPI, Region)
div.con.subset <- na.omit(div.con.subset)
detach(foreign.div.con)

div.con.subset$Arbitration <- 0

for (i in 1:nrow(div.con.subset)){
	if (div.con.subset$BIT_Strong_Arb[i]==1 | div.con.subset$Dom_I_Law_Arb[i]=="Comprehensive" | div.con.subset$Alt_Treaty_Arb[i]=="Comprehensive") 
	div.con.subset$Arbitration[i] <- 1
	else
	div.con.subset$Arbitration[i] <- 0
	}

z.out1 <- zelig(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + WB_Rule_Law + Polity, model="coxph", robust=TRUE, data=div.con.subset, cluster="Country") 
summary(z.out1)
extractAIC(z.out1)
x.low1 <- setx(z.out1, Arbitration=0) 
x.high1 <- setx(z.out1, Arbitration=1)
s.out1 <- sim(z.out1, x = x.low1, x1 = x.high1)
summary(s.out1)

##Survival Curves for Figure 7.1##

s.out <- coxph(Surv(Lifespan, Cancelled) ~ Arbitration + Polity + WB_Rule_Law + GDPpc1991, data=div.con.subset, robust=TRUE)
summary(survfit(s.out))
plot(survfit(s.out, conf.type = "log-log"))

par(mfrow=c(1,2))
plot(survfit(s.out, newdata=data.frame(Arbitration=1, Polity=6, WB_Rule_Law=-0.02, GDPpc1991=4092), conf.type = "log-log"), xlab="Year of Contract", ylab="Survival", main="Survival with Strong Arbitration")

plot(survfit(s.out, newdata=data.frame(Arbitration=0, Polity=6, WB_Rule_Law=-0.02, GDPpc1991=4092), conf.type = "log-log"), xlab="Year of Contract", ylab="Survival", main="Survival without Strong Arbitration")


##Table 7.6:  Contract Cancellation Rates for Foreign Investors by Level of Government, 1990-2008##

attach(foreign.div.con)
div.con.subset <- data.frame(Project_Name, Lifespan, Cancelled, BIT_Arb, BIT_Strong_Arb, Alt_Treaty_Arb, Dom_I_Law_Arb, GDPpc1991, WB_Rule_Law, Polity, Country,Type_PPI, Region, Grantee)
div.con.subset <- na.omit(div.con.subset)
detach(foreign.div.con)

##Calculating Rates for Strong Arbitration provisions##
for (i in 1:nrow(div.con.subset)){
	if (div.con.subset$BIT_Strong_Arb[i]==1 | div.con.subset$Dom_I_Law_Arb[i]=="Comprehensive" | div.con.subset$Alt_Treaty_Arb[i]=="Comprehensive") 
	div.con.subset$Arbitration[i] <- 1
	else
	div.con.subset$Arbitration[i] <- 0
	}

div.con.subset$National <- div.con.subset$Grantee=="Federal"

Local.contracts <- div.con.subset[div.con.subset$National==FALSE,]
nrow(Local.contracts)
Arb.Local.contracts <- Local.contracts[Local.contracts$Arbitration==1,]
nrow(Arb.Local.contracts)
No.Arb.Local.contracts <- Local.contracts[Local.contracts$Arbitration==0,]
nrow(No.Arb.Local.contracts)
Can.Arb.Local <- Arb.Local.contracts[Arb.Local.contracts$Cancelled==1,]
nrow(Can.Arb.Local)
Can.No.Arb <- No.Arb.Local.contracts[No.Arb.Local.contracts$Cancelled==1,]
nrow(Can.No.Arb)

National.contracts <- div.con.subset[div.con.subset$National==TRUE,]
nrow(National.contracts)
Arb.National.contracts <- National.contracts[National.contracts$Arbitration==1,]
nrow(Arb.National.contracts)
Can.Arb.National <- Arb.National.contracts[Arb.National.contracts$Cancelled==1,]
nrow(Can.Arb.National)
No.Arb.National <- National.contracts[National.contracts$Arbitration==0,]
nrow(No.Arb.National)

#Calculating Rates for Arbitration provisions of any strength##
div.con.subset$Arbitration <- 0

for (i in 1:nrow(div.con.subset)){
	if (div.con.subset$BIT_Arb[i]==1 | div.con.subset$Dom_I_Law_Arb[i]!=0 | div.con.subset$Alt_Treaty_Arb[i]!=0) 
	div.con.subset$Arbitration[i] <- 1
	else
	div.con.subset$Arbitration[i] <- 0
	}

div.con.subset$National <- div.con.subset$Grantee=="Federal"

Local.contracts <- div.con.subset[div.con.subset$National==FALSE,]
nrow(Local.contracts)
Arb.Local.contracts <- Local.contracts[Local.contracts$Arbitration==1,]
nrow(Arb.Local.contracts)
No.Arb.Local.contracts <- Local.contracts[Local.contracts$Arbitration==0,]
nrow(No.Arb.Local.contracts)
Can.Arb.Local <- Arb.Local.contracts[Arb.Local.contracts$Cancelled==1,]
nrow(Can.Arb.Local)
Can.No.Arb <- No.Arb.Local.contracts[No.Arb.Local.contracts$Cancelled==1,]
nrow(Can.No.Arb)

National.contracts <- div.con.subset[div.con.subset$National==TRUE,]
nrow(National.contracts)
Arb.National.contracts <- National.contracts[National.contracts$Arbitration==1,]
nrow(Arb.National.contracts)
Can.Arb.National <- Arb.National.contracts[Arb.National.contracts$Cancelled==1,]
nrow(Can.Arb.National)
No.Arb.National <- National.contracts[National.contracts$Arbitration==0,]
nrow(No.Arb.National)



##Analyses with Alternative Property Rights Variables##

attach(foreign.div.con)
div.con.subset <- data.frame(Project_Name, Lifespan, Cancelled, BIT_Arb, BIT_Strong_Arb, Alt_Treaty_Arb, Dom_I_Law_Arb, GDPpc1991, Prop_Rights, ICRG_Rule_Law, Gov.Effectiveness, Polity, Country,Type_PPI, Region, Joint_Venture)
div.con.subset <- na.omit(div.con.subset)
detach(foreign.div.con)

for (i in 1:nrow(div.con.subset)){
		if (div.con.subset$BIT_Strong_Arb[i]==1 | div.con.subset$Dom_I_Law_Arb[i]=="Comprehensive" | div.con.subset$Alt_Treaty_Arb[i]=="Comprehensive") 
		div.con.subset$Arbitration[i] <- 1
		else
		div.con.subset$Arbitration[i] <- 0
		}


z.out <- zelig(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + ICRG_Rule_Law + Polity + strata(Type_PPI), model="coxph", data=div.con.subset, robust=TRUE, cluster="Country")
summary(z.out)	

z.out <- zelig(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + Prop_Rights + Polity + strata(Type_PPI), model="coxph", data=div.con.subset, robust=TRUE, cluster="Country")
summary(z.out)

s.out <- coxph(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + ICRG_Rule_Law + Polity + strata(Type_PPI) + cluster(Country), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)

s.out <- coxph(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + Prop_Rights + Polity + strata(Type_PPI) + cluster(Country), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)

s.out <- coxph(Surv(Lifespan, Cancelled) ~ Arbitration + GDPpc1991 + Prop_Rights + Gov.Effectiveness + Polity +  strata(Type_PPI) + cluster(Country), robust=TRUE, data=div.con.subset)
summary(s.out)
extractAIC(s.out)
cox.zph(s.out)


